<?php

/**
 * This is the model class for table "{{cporder}}".
 *
 * The followings are the available columns in table '{{cporder}}':
 * @property string $idcporder
 * @property string $idcpquotation
 * @property string $idcpordertype
 * @property string $orderdate
 * @property string $status
 * @property string $idcpsupplier
 *
 * The followings are the available model relations:
 * @property Cpbill[] $cpbills
 * @property Cpordertype $idcpordertype0
 * @property Cpquotation $idcpquotation0
 * @property Cpsupplier $idcpsupplier0
 * @property Cporderitem[] $cporderitems
 * @property Swstockreceived[] $swstockreceiveds
 */
class Cporder extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Cporder the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{cporder}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('idcpquotation, idcpordertype, orderdate, status, idcpsupplier', 'required'),
			array('idcpquotation, idcpordertype, idcpsupplier', 'length', 'max'=>10),
			array('status', 'length', 'max'=>45),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('idcporder, idcpquotation, idcpordertype, orderdate, status, idcpsupplier', 'safe', 'on'=>'search'),
                        array('idcpquotation, idcpordertype, idcpsupplier', 'validatefkeys'),
		);
	}        
        
        public function validatefkeys($attribute,$params){
               $aux = $attribute . '0';
               if(!$this->$aux)
                    $this->addError($attribute, Yii::t('application', 'Incorrect {attribute}.', array('{attribute}'=>Yii::t('application', $attribute))));
        }      
        
        
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'cpbills' => array(self::HAS_MANY, 'Cpbill', 'idcporder'),
			'idcpordertype0' => array(self::BELONGS_TO, 'Cpordertype', 'idcpordertype'),
			'idcpquotation0' => array(self::BELONGS_TO, 'Cpquotation', 'idcpquotation'),
			'idcpsupplier0' => array(self::BELONGS_TO, 'Cpsupplier', 'idcpsupplier'),
			'cporderitems' => array(self::HAS_MANY, 'Cporderitem', 'idcporder'),
			'swstockreceiveds' => array(self::HAS_MANY, 'Swstockreceived', 'idcporder'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'idcporder' => Yii::t('application', 'Idcporder'),
			'idcpquotation' => Yii::t('application', 'Idcpquotation'),
			'idcpordertype' => Yii::t('application', 'Idcpordertype'),
			'orderdate' => Yii::t('application', 'Orderdate'),
			'status' => Yii::t('application', 'Status'),
			'idcpsupplier' => Yii::t('application', 'Idcpsupplier'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('idcporder',$this->idcporder,true);
		$criteria->compare('idcpquotation',$this->idcpquotation,true);
		$criteria->compare('idcpordertype',$this->idcpordertype,true);
		$criteria->compare('orderdate',$this->orderdate,true);
		$criteria->compare('status',$this->status,true);
		$criteria->compare('idcpsupplier',$this->idcpsupplier,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
}